<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ include file="/common/CimsCommon.jsp"%>
<link rel="stylesheet" href="<%=basePath%>/css/cims/cmmBase.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>

<body style="background-color:#FFFFFF">
	<div id="formPageSwap">
		<br />
		<form id="addNewsFormData" name="addNewsFormData" isCheck="true"
			action="com.jy.modules.common.tcmmquota.controller.TCMmQuotaController">
			<div class="expandSwap">
				<div class="ui-widget-header ui-corner-all">
					<span class="text">选择机构</span> 
				</div>
				<div class="expandContent">
					 <table id="updateNewsTableId" class="formTableSwap" border="0" align="center" cellpadding="0" cellspacing="1" >
						<tr>
						  <th> 合作机构 ：</th>
						  <td> 
						  	  <select id="dtocooperorgCode" onchange="searchDealForCreditBuyBycooperorgCode(this);" ></select><%--
							  <input type="hidden" class="text" id="dtocooperorgCode" name="cooperorgCode" notNull="false" value="${contractAllDto.cdto.cooperorgCode}" readonly/>
							  <input type="text" class="text cims_noborder" id="dtocooperorgName" name="cooperorgName" notNull="false" maxLength="16" value="${contractAllDto.cdto.cooperorgCode}" readonly/>
						  --%></td>
						  <th></th>
						  <td></td>
						  <th></th>
						  <td></td>
						  
						</tr>
					</table>
				</div>
				<div id="list"></div>

				<div class="tableToolbar">
					<a href="javascript:void(0)" onclick="trial()">试算</a>
				</div>

				<div class="ui-widget-header ui-corner-all">
					<span class="text">结算信息</span>
				</div>
				<div class="expandContent">
					<table class="formTableSwap" border="0" align="center"
						cellpadding="0" cellspacing="1">
						<input type="text"  style="display:none;"  name="settleDealCodes" id="settleDealCodes" >
						<tr>
							<th>结算金额 ：</th>
							<td><input type="text" class="text cims_noborder"
								id="settleSum" name="" maxLength="16" value="" readonly /></td>
							<th>需匹配金额：</th>
							<td><input type="text" class="text cims_noborder" id="planSellSum"
								name="planSellSum"  value=""
								onmouseout="searchBuy()" readonly /></td>
							<th>返还金额：</th>
							<td><input type="text" class="text cims_noborder" id="deliverSum"
								name="" value="" readonly /></td>
						</tr>
						<tr>
							<th>选择交易条数：</th>
							<td><input type="text" class="text cims_noborder"
								id="dealRows" name="" maxLength="4" value="" checkType="number"
								readonly /></td>
							<th>实际匹配金额 ：</th>
							<td><input type="text" class="text" notNull="false" checkType="number"
								id="practicalSellSum" name="practicalSellSum"  value=""  /></td>
							<th></th>
							<td></td>
						</tr>
					</table>
				</div>
				<div class="expandToolbar">
					<span class="submitBtn" onclick="toDeal();">购买</span> <span
						class="submitBtn" onclick="parent.tabs.close();">关闭</span>
				</div>
			</div>
			<!-- 保存 关闭 按钮 在 查询页面进行控制 -->
		</form>

	</div>
	<script type="text/javascript"
		src="<%=basePath%>/js/cims/publicJs/importContract.js"></script>
</body>

<%@include file="settleList.jsp"%>
<script type="text/javascript">
   $(document).ready(function(){
   		checkedInit();//平台校验不兼容IE
   		var dtocooperorgCode=$("#dtocooperorgCode"),
   			table;
   		
   		if(table){
	    	table.url=table.url.split("=")[0]+"=000";
	    	table.query();
	    }
   		//加载机构信息
   		var dataUrl=contextRootPath+"/cimsCascade/queryListCimsCascadeCooperorg";
   		$.ajax({
   			url: dataUrl,
   			type: 'get',
   			dataType: 'json',
   			data:'',
   			error: function(result) {
   				alert("请查看返回数据是否正确：" + result);
   			},
   			success: function(results) {
   				var result=results.data;
   				var j;
   				var resultLength = result.length - 1;
   				dtocooperorgCode.append("<option id='selectValue' value='0'>--请选择--</option>");
   				for (j = 0; j <= resultLength; j++) {
   					dtocooperorgCode.append("<option value='"+result[j].cooperorgCode+"'>"+result[j].cooperorgName+"</option>");
   				}
   			}
   		});
   		
	});
	
	function trial(){
		var table=iframe.iframeObj["table"],
			ids=table.getSelectedIds(),
			dataMap=table.dataMap,
			settleSum=0,
			planSellSum=0,
			practicalSellSum=0;
			deliverSum = 0;
			settleDealCodes="";
		if(ids==''){
		  alert('请从交易流水列表中选择流水');
		  return;
		}
		
		$.each(ids,function(i,n){
			var record=dataMap[n];
			planSellSum += typeof record.planSellSum=="number"?record.planSellSum*100:0;
			practicalSellSum += typeof record.practicalSellSum=="number"?record.practicalSellSum*100:0;
			settleSum += typeof record.sumCreditorValue=="number"?record.sumCreditorValue*100:0;
			deliverSum += typeof record.deliverSum=="number"?record.deliverSum*100:0;
			settleDealCodes += record.dealCode+",";
		});
		planSellSum = planSellSum/100;	
		practicalSellSum = practicalSellSum/100;
		settleSum = settleSum/100;
		deliverSum = deliverSum/100;
		$("#settleSum").val(settleSum);//结算金额
		$("#deliverSum").val(deliverSum);//返还金额
		$("#settleDealCodes").val(settleDealCodes);
		$("#planSellSum").val(planSellSum);
		$("#practicalSellSum").val(practicalSellSum);
		$("#dealRows").val(ids.length);
	}
	
	function toDeal(){
		var billDates = iframe.iframeObj["table"].getSelectedObjs("billDate");
		var creditPackageName = $("#creditPackage").find("option:selected").val();
		var billDate = billDates[0];
		if (!CheckUtil.formCheck("addNewsFormData")) {
			return;
		}
		
		var settleDealCodes = $('#settleDealCodes').val();
	    var planSellSum = $('#planSellSum').val();
	    var practicalSellSum = $('#practicalSellSum').val();
	
		if(!settleDealCodes){
		  alert("请选择结算流水进行试算");
		  return;
		}
	  if(confirm("请确认选择的交易流水和结算信息无误后，再购买！")){
		    var cooperorgCode = $("#dtocooperorgCode").find("option:selected").val();
		window.location=contextRootPath+"/creditBuy/prepareExecute/toBackDeal?cooperorgCode="+cooperorgCode+"&settleDealCodes="+settleDealCodes+"&planSellSum="+planSellSum+"&practicalSellSum="+practicalSellSum;
	  }
	}
	
	//查询债权包	
  function searchCreditPackage(){
	var valueCooperorgCode = $('#dtocooperorgCode').val();
	var contractCode =  $('#dtocontractCode').val();
	if(valueCooperorgCode==""|| null ==valueCooperorgCode){
		$('#creditPackage').html("<option id='selectValue' value='0'>--请选择--</option>");
		return;
	}
	
	var dataUrl = contextRootPath + '/cooperorgDeal/getPackageList?cooperorgCode=' + valueCooperorgCode+'&contractCode='+contractCode +'&pagType='+cimsConstant.CREDIT_RESOURCE;
	$('#creditPackage').html("<option id='selectValue' value='0'>--请选择--</option>");
	$.ajax({
		url: dataUrl,
		type: 'get',
		dataType: 'json',
		data:'',
		error: function(result) {
			alert("请查看返回数据是否正确：" + result);
		},
		success: function(results) {
			var result=results.data;
			var j;
			var resultLength = result.length - 1;
			for (j = 0; j <= resultLength; j++) {
				$('#creditPackage').append("<option value='"+result[j].creditPackageName+"'>"+result[j].creditPackageName+"</option>");
			}
		}
	});

}


function searchDealForCreditBuyBycooperorgCode(obj){
    var cooperorgCode = $("#dtocooperorgCode").find("option:selected").val();
//	var cooperorgCode = $('#dtocooperorgCode').val();
//	var contractCode =  $('#dtocontractCode').val();
	
	var dataUrl = contextRootPath + '/creditBuy/queryAllTypeSettle?cooperorgCode='+cooperorgCode+'&dealType='+cimsConstant.DEAL_TYPE_MONTH_BACK;
	var table=iframe.iframeObj["table"];
	table.url=dataUrl;
	table.query(); 
}

function searchBuy(){
   
}
</script>
</html>
